package com.qf.controller;

import com.qf.entity.SysUser;

import com.qf.service.SysUserService;
import com.qf.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashMap;

/**
 * --- 天道酬勤 ---
 *
 * @author QiuShiju
 * @date 2025/10/10
 * @desc 注册接口
 */
@RestController
public class Demo4Controller {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private JwtUtils jwtUtils;

    /**
     * 注册接口
     */
    @PostMapping("/register")
    public String register(@RequestBody SysUser sysUser) {
        sysUserService.register(sysUser);
        return "register";
    }

    @GetMapping("/info")
    public String getInfo(){
        // 【重点方法0】可以在任何地方获取当前登录用户的 Authentication 对象
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

        if (authentication != null) {
            // 【重点方法1】 获取当前登录用户的权限集合
            Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
            System.out.println("authorities = " + authorities);

            // 【重点方法2】 获取当前登录用户的用户名
            String name = authentication.getName(); // 获取用户名
            System.out.println("name = " + name);

            Object details = authentication.getDetails(); // 获取用户详细信息(几乎不用)
            System.out.println("details = " + details);

            // 【重点方法3】 获取当前登录用户的用户对象(用户详情对象)
            Object principal = authentication.getPrincipal();
            if (principal instanceof UserDetails) {
                UserDetails userDetails = (UserDetails) principal;
                System.out.println("principal(UserDetails) = " + userDetails);
            } else {
                System.out.println("principal = " + principal);
            }

            // 注意：getCredentials() 在认证成功前有值, 认证成功后通常返回 null
            // String credentials = authentication.getCredentials().toString(); // 获取密码
            // System.out.println("credentials(password) = " + credentials);
        } else {
            System.out.println("用户未登录");
        }

        return "获取已登录的用户信息";
    }

    // 测试发请求,产生令牌
    @GetMapping("/create")
    public String create(){
        HashMap<String, Object> map = new HashMap<>();
        map.put("password","123456");
        map.put("role","超级管理员");
        map.put("avatar","http://www.baidu.touxing.png");
        String token = jwtUtils.createToken(map);

        return token;
    }

    // 解析令牌
    @GetMapping("/parse")
    public void parse(String token){
        Claims claims = jwtUtils.parseToken(token);
        System.out.println("claims = " + claims);
    }
}